package com.lixinkeji.extension

import android.animation.Animator
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.drake.net.utils.scopeNet
import com.drake.net.utils.scopeNetLife
import com.lixinkeji.base.R
import com.lixinkeji.extension.applyVisible
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import per.goweii.anylayer.AnyLayer
import per.goweii.anylayer.Layer
import per.goweii.anylayer.ktx.doOnClickToDismiss
import per.goweii.anylayer.ktx.setContentAnimator
import per.goweii.anylayer.ktx.setSwipeDismiss
import per.goweii.anylayer.utils.AnimatorHelper
import per.goweii.anylayer.widget.SwipeLayout
import java.io.ByteArrayOutputStream


/**
 *
 * @ClassName Dialog
 * @Author lwc
 * @Email 1679114787@qq.com
 * @Date 2022/1/4 11:16
 *
 */
fun Context.commonLineDialog(
    content: String = "",
    cancel: String = "",
    sure: String = "",
    onCancel: () -> Unit = {},
    onSure: () -> Unit = {}
) {
    AnyLayer.dialog(this).contentView(R.layout.common_dialog_line).backgroundDimAmount(0.5f)
        .bindData {
            val contentView = it.getView<TextView>(R.id.content)
            val sureView = it.getView<TextView>(R.id.sure)
            val cancelView = it.getView<TextView>(R.id.cancel)
            sureView.applyVisible(sure.isNotEmpty())
            cancelView.applyVisible(cancel.isNotEmpty())
            contentView.applyVisible(content.isNotEmpty())
            contentView?.text = content
            sureView?.text = sure
            cancelView?.text = cancel
        }.doOnClickToDismiss(R.id.cancel) { onCancel.invoke() }
        .doOnClickToDismiss(R.id.sure) { onSure.invoke() }.show()
}


fun Context.commonLineOneButtonDialog(
    content: String = "",
    sure: String = "",
    onSure: () -> Unit = {}
) {
    AnyLayer.dialog(this).contentView(R.layout.common_dialog_line_one_button)
        .backgroundDimAmount(0.5f)
        .bindData {
            val contentView = it.getView<TextView>(R.id.content)
            val sureView = it.getView<TextView>(R.id.sure)
            sureView.applyVisible(sure.isNotEmpty())
            contentView.applyVisible(content.isNotEmpty())
            contentView?.text = content
            sureView?.text = sure
        }.doOnClickToDismiss(R.id.sure) { onSure.invoke() }.show()

}


fun Context.commonDialog(
    title: String,
    content: String = "",
    cancel: String = "",
    sure: String = "",
    onCancel: (() -> Unit)? = null,
    onSure: (() -> Unit)? = null,
    ll_bg: Int? = null
) {
    AnyLayer.dialog(this).contentView(R.layout.common_dialog).outsideTouchedToDismiss(false)
        .cancelableOnClickKeyBack(false).backgroundDimAmount(0.5f).bindData {
            val contentView = it.getView<TextView>(R.id.content)
            val titleView = it.getView<TextView>(R.id.title)
            val sureView = it.getView<TextView>(R.id.sure)
            val cancelView = it.getView<TextView>(R.id.cancel)

            sureView.applyVisible(sure.isNotEmpty())
            cancelView.applyVisible(cancel.isNotEmpty())
            contentView.applyVisible(content.isNotEmpty())
            contentView?.text = content
            titleView?.text = title
            sureView?.text = sure
            cancelView?.text = cancel
        }.doOnClickToDismiss(R.id.cancel) { if (onCancel != null) onCancel() }
        .doOnClickToDismiss(R.id.sure) { if (onSure != null) onSure() }.show()
}


//fun Context.cancelDialog(
//    title: String,
//    content: String = "",
//    cancel: String = "",
//    sure: String = "",
//    onCancel: (() -> Unit)? = null,
//    onSure: (() -> Unit)? = null,
//    ll_bg: Int? = null
//) {
//    AnyLayer.dialog(this).contentView(R.layout.first_login_dialog_cancel)
//        .outsideTouchedToDismiss(false)
//        .cancelableOnClickKeyBack(false)
//        .bindData {
//            val contentView = it.getView<TextView>(R.id.content)
//            val titleView = it.getView<TextView>(R.id.title)
//            val sureView = it.getView<TextView>(R.id.sure)
//            val cancelView = it.getView<TextView>(R.id.cancel)
//
//            sureView.applyVisible(sure.isNotEmpty())
//            cancelView.applyVisible(cancel.isNotEmpty())
//            contentView.applyVisible(content.isNotEmpty())
//            contentView?.text = content
//            titleView?.text = title
//            sureView?.text = sure
//            cancelView?.text = cancel
//        }.doOnClickToDismiss(R.id.cancel) { if (onCancel != null) onCancel() }
//        .doOnClickToDismiss(R.id.sure) { if (onSure != null) onSure() }.show()
//}


//fun Context.firstLoginDialog(
//    title: String,
//    content: String = "",
//    cancel: String = "",
//    sure: String = "",
//    onCancel: (() -> Unit)? = null,
//    onSure: (() -> Unit)? = null,
//) {
//    AnyLayer.dialog(this).contentView(R.layout.first_login_dialog)
//        .outsideTouchedToDismiss(false)
//        .cancelableOnClickKeyBack(false)
//        .bindData {
//            val contentView = it.getView<TextView>(R.id.content)
//            val titleView = it.getView<TextView>(R.id.title)
//            val sureView = it.getView<TextView>(R.id.sure)
//            val cancelView = it.getView<TextView>(R.id.cancel)
//            val mStyledText = SpannableString(content)
//            mStyledText.setSpan(
//                ForegroundColorSpan(Color.parseColor("#2079f3")),
//                9,
//                21,
//                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
//            )
//            mStyledText.setSpan(
//                ForegroundColorSpan(Color.parseColor("#2079f3")),
//                23,
//                30,
//                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
//            )
//            lateinit var helpBean: HelpBean
//            scopeNet {
//                helpBean = help_feedback(1)!!
//            }
//            val mPattern1: Pattern = Pattern.compile("《智慧课堂APP用户服务协议》")
//            val mPattern2: Pattern = Pattern.compile("《智慧课堂隐私政策》")
//            val mMatcher1: Matcher = mPattern1.matcher(content)
//            val mMatcher2: Matcher = mPattern2.matcher(content)
//            while (mMatcher1.find()) {
//                val what = object : ClickableSpan() {
//                    override fun onClick(widget: View) {
//                        start(PrivacyWebActivity::class.java) {
//                            putExtra("title", "用户协议")
//                            putExtra("url", "cs1")
//                            putExtra("type", 1)
//
//                        }
//                    }
//                }
//                mStyledText.setSpan(
//                    what,
//                    mMatcher1.start(),
//                    mMatcher1.end(),
//                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//                );
//                if (contentView != null) {
//                    contentView.text = mStyledText
//                    contentView.movementMethod = LinkMovementMethod.getInstance();
//                }
//            }
//
//            while (mMatcher2.find()) {
//                val what = object : ClickableSpan() {
//                    override fun onClick(widget: View) {
//                        start(PrivacyWebActivity::class.java) {
//                            putExtra("title", "隐私政策")
//                            putExtra("url", "cs2")
//                            putExtra("type", 1)
//
//                        }
//                    }
//                }
//                mStyledText.setSpan(
//                    what,
//                    mMatcher2.start(),
//                    mMatcher2.end(),
//                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//                );
//                if (contentView != null) {
//                    contentView.text = mStyledText
//                    contentView.setMovementMethod(LinkMovementMethod.getInstance());
//                }
//            }
//
//
//
//            sureView.applyVisible(sure.isNotEmpty())
//            cancelView.applyVisible(cancel.isNotEmpty())
//            contentView.applyVisible(content.isNotEmpty())
//            contentView?.text = mStyledText
//            titleView?.text = title
//            sureView?.text = sure
//            cancelView?.text = cancel
//        }.doOnClickToDismiss(R.id.cancel) { if (onCancel != null) onCancel() }
//        .doOnClickToDismiss(R.id.sure) { if (onSure != null) onSure() }.show()
//}


//fun Context.setBuildFinishDialog(
//    title: String,
//    content: String = "",
//    cancel: String = "",
//    sure: String = "",
//    onCancel: (() -> Unit)? = null,
//    onSure: (() -> Unit)? = null
//
//) {
//    AnyLayer.dialog(this).contentView(R.layout.dialog_setbuild_finish).backgroundDimAmount(0.5f)
//        .bindData {
//            val ll = it.getView<RelativeLayout>(R.id.ll)
//            val contentView = it.getView<TextView>(R.id.content)
//            val titleView = it.getView<TextView>(R.id.title)
//            val sureView = it.getView<TextView>(R.id.sure)
//            val cancelView = it.getView<TextView>(R.id.cancel)
//            sureView.applyVisible(sure.isNotEmpty())
//            cancelView.applyVisible(cancel.isNotEmpty())
//            contentView.applyVisible(content.isNotEmpty())
//            contentView?.text = content
//            titleView?.text = title
//            sureView?.text = sure
//            cancelView?.text = cancel
//        }.doOnClickToDismiss(R.id.cancel) { if (onCancel != null) onCancel() }
//        .doOnClickToDismiss(R.id.sure) { if (onSure != null) onSure() }.show()
//}

//fun Context.changeBuildFinishDialog(
//    title: String,
//    content: String = "",
//    cancel: String = "",
//    sure: String = "",
//    onCancel: (() -> Unit)? = null,
//    onSure: (() -> Unit)? = null
//
//) {
//    AnyLayer.dialog(this).contentView(R.layout.dialog_changebuild_finish).backgroundDimAmount(0.5f)
//        .bindData {
//            val ll = it.getView<RelativeLayout>(R.id.ll)
//            val contentView = it.getView<TextView>(R.id.content)
//            val titleView = it.getView<TextView>(R.id.title)
//            val sureView = it.getView<TextView>(R.id.sure)
//            val cancelView = it.getView<TextView>(R.id.cancel)
//            sureView.applyVisible(sure.isNotEmpty())
//            cancelView.applyVisible(cancel.isNotEmpty())
//            contentView.applyVisible(content.isNotEmpty())
//            contentView?.text = content
//            titleView?.text = title
//            sureView?.text = sure
//            cancelView?.text = cancel
//        }.doOnClickToDismiss(R.id.cancel) { if (onCancel != null) onCancel() }
//        .doOnClickToDismiss(R.id.sure) { if (onSure != null) onSure() }.show()
//}


fun Context.bottomDialog(
    one: String,
    oneColor: Int = R.color.text_333,
    two: String = "",
    twoColor: Int = R.color.text_333,
    three: String = "",
    threeColor: Int = R.color.text_333,
    cancel: String = "取消",
    cancelColor: Int = R.color.text_999,
    onOne: (() -> Unit)? = null,
    onTwo: (() -> Unit)? = null,
    onThree: (() -> Unit)? = null,
    onCancel: (() -> Unit)? = null
) {
    AnyLayer.dialog(this)
        .contentView(R.layout.bottoim_dialog)
        .gravity(Gravity.BOTTOM)
        .setSwipeDismiss(SwipeLayout.Direction.BOTTOM)
        .setContentAnimator(object :
            Layer.AnimatorCreator {
            override fun createInAnimator(target: View): Animator {
                return AnimatorHelper.createBottomInAnim(target)
            }

            override fun createOutAnimator(target: View): Animator {
                return AnimatorHelper.createBottomOutAnim(target)
            }

        }).backgroundDimAmount(0.5f).bindData {
            val oneView = it.getView<TextView>(R.id.one)
            val twoView = it.getView<TextView>(R.id.two)
            val threeView = it.getView<TextView>(R.id.three)
            val cancelView = it.getView<TextView>(R.id.cancel)
            oneView?.setTextColor(getColor(oneColor))
            cancelView?.setTextColor(getColor(cancelColor))
            oneView?.text = one
            cancelView?.text = cancel
            twoView.applyVisible(two.isNotEmpty())
            threeView.applyVisible(three.isNotEmpty())
            twoView?.text = two
            threeView?.text = three
        }.doOnClickToDismiss(R.id.one) { if (onOne != null) onOne() }
        .doOnClickToDismiss(R.id.two) { if (onTwo != null) onTwo() }
        .doOnClickToDismiss(R.id.three) { if (onThree != null) onThree() }
        .doOnClickToDismiss(R.id.cancel) { if (onCancel != null) onCancel() }.show()
}













